package mobile.couponow;
/*
 */

import java.io.IOException;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Helper class to handle BroadcastReciver behavior.
 * - can only run for a limited amount of time - it must start a real service 
 * for longer activity
 * - must get the power lock, must make sure it's released when all done.
 * 
 */
public class C2DMReceiver extends BroadcastReceiver {

	private static final String TAG = "RECEIVER";

	@Override
	public final void onReceive(Context context, Intent intent) {
		// To keep things in one place.
		Log.w("C2DMReceiver-onRegistered", intent.getAction());
		//Intent serviceIntent = new Intent(intent.getAction());
		if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
			handleMessage(context, intent);
		}
		setResult(Activity.RESULT_OK, null /* data */, null /* extra */);			
	}

	private void handleMessage(Context context, Intent intent) {
		Intent messageIntent = new Intent("mobile.couponow.RECEIVE");
		context.startService(messageIntent);

	}

	private void handleRegistration(final Context context, Intent intent) {
		final String registrationId = intent.getStringExtra("registration_id");
		String error = intent.getStringExtra("error");
		String removed = intent.getStringExtra("unregistered");
		Log.w(TAG, "Inside HandleRegistration  " + registrationId);

		if (Log.isLoggable(TAG, Log.DEBUG)) {
			Log.d(TAG, "dmControl: registrationId = " + registrationId +
					", error = " + error + ", removed = " + removed);
		}

		if (removed != null) {
			// Remember we are unregistered
			C2DMessaging.clearRegistrationId(context);
			context.startService(new Intent("mobile.couponow.UNREGISTER"));
			return;
		} else if (error != null) {
			// we are not registered, can try again
			C2DMessaging.clearRegistrationId(context);
			// Registration failed
			Log.e(TAG, "Registration error " + error);
			if ("SERVICE_NOT_AVAILABLE".equals(error)) {
				long backoffTimeMs = C2DMessaging.getBackoff(context);

				Log.d(TAG, "Scheduling registration retry, backoff = " + backoffTimeMs);
				Intent retryIntent = new Intent("com.google.android.c2dm.intent.RETRY");
				PendingIntent retryPIntent = PendingIntent.getBroadcast(context, 
						0 /*requestCode*/, retryIntent, 0 /*flags*/);

				AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
				am.set(AlarmManager.ELAPSED_REALTIME,
						backoffTimeMs, retryPIntent);

				// Next retry should wait longer.
				backoffTimeMs *= 2;
				C2DMessaging.setBackoff(context, backoffTimeMs);
			} 
		} else {
			try {
			Log.w("C2DMReceiver-onRegistered", "inside regIntent");
			C2DMessaging.setRegistrationId(context, registrationId);
			Intent serviceIntent = new Intent("mobile.couponow.REGISTRATION");
			serviceIntent.putExtra("registration_id", registrationId);
			Log.w("C2DMReceiver-onRegistered", "before StartService");
			context.startService(serviceIntent);
			}
			catch (Exception e){
				e.printStackTrace();
			}
		}
	}
}